#include "sendok.h"
#include "ui_sendok.h"

sendok::sendok(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::sendok)
{
    ui->setupUi(this);
    So = "N";
    connect(ui->So, &QRadioButton::clicked, this, &sendok::SSS);
    connect(ui->cancel, &QPushButton::clicked, this, &sendok::cc);
    connect(ui->YesButton, &QPushButton::clicked, this, &sendok::save);
}

sendok::~sendok()
{
    delete ui;
}

void sendok::cc() {
    this->close();
}

void sendok::save() {
    QString OrderID = ui->OrderID->text();
    QDateEdit *dateEdit = ui->SDate;
    QString SDate = dateEdit->date().toString("yyyy-MM-dd");
    QString SCount = ui->SCount->text();


    // 验证输入不为空
    if (OrderID.isEmpty() || SDate.isEmpty() || SCount.isEmpty()) {
        QMessageBox::warning(this, "错误", "请填写所有必要信息！");
        return;
    }

    // 执行SQL插入
    QSqlDatabase db = QSqlDatabase::database("qt_sql_default_connection");
    if (!db.isOpen()) {
        QMessageBox::warning(this, "错误", "数据库未连接！");
        return;
    }

    QSqlQuery query(db);
    query.prepare("INSERT INTO SendList (OrderID, SDate, SCount, So) "
                  "VALUES (?, ?, ?, ?)");
    query.addBindValue(OrderID);
    query.addBindValue(SDate);
    query.addBindValue(SCount);
    query.addBindValue(So);

    query.prepare("DELETE FROM OrderList WHERE OrderID = ?");
    query.addBindValue(OrderID);

    if (query.exec()) {
        QMessageBox::information(this, "成功", "数据更新成功！");
        this->close();  // 关闭窗口
    } else {
        QMessageBox::critical(this, "错误",
                              QString("数据更新失败。请检查填写信息！"));
    }
}

void sendok::SSS(bool checked) {
    So = checked ? "Y" : "N";
}
